package com.guragura.util;

import java.util.List;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.guragura.screens.GameScreen;
import com.guragura.widget.HighScorePanel;
import com.guragura.widget.ScorePanel;


/**
 * @author hartono
 * Kelas ini digunakan untuk menuliskan angka, baik angka untuk score pada {@link GameScreen}, maupun tulisan
 * highscore pada {@link HighScorePanel} dan {@link ScorePanel}.
 */
public class GuraFont {

	public List<TextureAtlas.AtlasRegion> numsc;
	public List<TextureAtlas.AtlasRegion> numhs;
	
	private SpriteBatch batch;
	
	/**
	 *  Jenis font yang digunakan ditentukan oleh gambar yang berisi 1,2,3 .. 9 
	 * @param assetManager
	 * @param batch
	 */
	public GuraFont(AssetManager assetManager, SpriteBatch batch) {
		// TODO Auto-generated constructor stub
		numsc = assetManager.get(Constants.PACK_INTERFACE, TextureAtlas.class).findRegions(Constants.NUMSC);
		numhs = assetManager.get(Constants.PACK_INTERFACE, TextureAtlas.class).findRegions(Constants.NUMHS);		
		this.batch = batch;
	}
	
	/**
	 * untuk menulis angka biasa, untuk score pada {@link GameScreen}
	 * @param string
	 * @param x
	 * @param y
	 */
	public void draw(String string , int x, int y){
		batch.begin();
		for (int i= 0; i< string.length();i++){
			char c = string.charAt(i);
			try{
				int num = Integer.parseInt(c+"");
				TextureRegion reg = numsc.get(num);
				batch.draw(reg, x, y);
				x+=reg.getRegionWidth();
			}catch(Exception e){
				x+=5;
			}			
		}
		batch.end();
	}
	
	/**
	 * untuk menulis angka pada {@link HighScorePanel} ataupun {@link ScorePanel}
	 * @param string
	 * @param x
	 * @param y
	 */
	public void drawHS(String string, int x, int y){
		batch.begin();
		for (int i= 0; i< string.length();i++){
			char c = string.charAt(i);
			try{
				int num = Integer.parseInt(c+"");
				TextureRegion reg = numhs.get(num);
				batch.draw(reg, x, y);
				x+=reg.getRegionWidth();
			}catch(Exception e){
				x+=5;
			}			
		}
		batch.end();
	}
	
}
